package com.alm.token.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.AddUpCloseVipData;
import com.alm.token.ui.data.bean.CurrentOrderData;
import com.alm.token.ui.data.bean.EggRankingData;
import com.alm.token.ui.data.bean.EggWalletLogData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.SuperColorEggData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.SuperColorEggView;
import com.alm.token.ui.view.VipApplyView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.widget.PayPasswordView;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.ArrayList;
import java.util.List;

import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BuySEggActivity extends BaseActivity implements SuperColorEggView<SuperColorEggData> {


    @BindView(R.id.img_bizhongicon)
    ImageView imgBizhongicon;
    @BindView(R.id.tv_bizhongtv)
    TextView tvBizhongtv;
    @BindView(R.id.tv_totalmoney)
    TextView tvTotalmoney;
    @BindView(R.id.img_selectbizhong)
    ImageView imgSelectbizhong;
    @BindView(R.id.ly_ooutmoneybizhongtop)
    RelativeLayout lyOoutmoneybizhongtop;
    @BindView(R.id.tv_getseth)
    TextView tvGetseth;
    @BindView(R.id.tv_buymsg)
    TextView tvBuymsg;
    @BindView(R.id.tv_buycommit)
    TextView tvBuycommit;
    private List<SuperColorEggData.DataBean.EggGoodsBean> eggGoodsBeans = new ArrayList<>();
    private SuperColorEggData.DataBean dataBean;
    private int index = 0;

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_buyegg;
    }

    @Override
    protected void initVariables() {
        eggGoodsBeans = (List<SuperColorEggData.DataBean.EggGoodsBean>) getIntent().getSerializableExtra("eggGoodsBeans");
    }

    @Override
    protected void initViews() {
//        if (intentType == 1) {
        new ActionBarHelper(this).setTitle(getString(R.string.easteregg_hint_buyegg));
        tvBizhongtv.setText("");
        tvTotalmoney.setText("");
        imgSelectbizhong.setVisibility(View.INVISIBLE);
        tvGetseth.setText("");
        tvBuymsg.setText("");
    }

    @Override
    protected void loadData() {
//        if (eggGoodsBeans == null)
        preenter.getSuperColorEggData();
//        else
//            initView();
    }

    private void initView() {
        if (eggGoodsBeans.size() > 0) {
            GlideApp.with(BuySEggActivity.this).load("")
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                            if (EmptyUtils.isNotEmpty(resource)) {
//                                CommonUtil. changeSVGColor(BuySEggActivity.this, imgBizhongicon, resource, R.color.black);
//                                return true;
//                            }
                            return false;
                        }
                    })
                    .error(R.drawable.icon_arctic).into(imgBizhongicon);
            tvBizhongtv.setText(eggGoodsBeans.get(index).getName());
            tvTotalmoney.setText(CommonUtil.saveToPoint4WithNo(eggGoodsBeans.get(index).getEth()) + "MIO");
            imgSelectbizhong.setVisibility(eggGoodsBeans.size() > 1 ? View.VISIBLE : View.INVISIBLE);
            tvGetseth.setText(getString(R.string.easteregg_hint_getegg1) + CommonUtil.saveToPoint4WithNo(eggGoodsBeans.get(index).getGetSeth()) + " eMIO");
        }
        if (EmptyUtils.isNotEmpty(dataBean)) {
            tvBuymsg.setText(dataBean.getMsg());
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void getData(SuperColorEggData superColorEggData) {
        if (superColorEggData.getStatus() == 0) {

            if (superColorEggData.getData() != null) {
                dataBean = superColorEggData.getData();
                eggGoodsBeans = superColorEggData.getData().getEggGoods();
                if (EmptyUtils.isNotEmpty(eggGoodsBeans) && eggGoodsBeans.size() > 0) {
                    initView();
                }
            } else showToast(superColorEggData.getMsg());
        } else showToast(superColorEggData.getMsg());
    }

    @Override
    public void getEggAddData(RegisterData registerData) {
        showToast(registerData.getMsg());
        if (registerData.getStatus() == 0) {
            finish();
        }
    }

    @Override
    public void getEggRankingData(EggRankingData eggRankingData) {

    }

    @Override
    public void getEggWalletLogData(EggWalletLogData eggWalletLogData) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ly_ooutmoneybizhongtop, R.id.tv_buycommit})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.ly_ooutmoneybizhongtop:
                if (EmptyUtils.isNotEmpty(eggGoodsBeans) && eggGoodsBeans.size() > 0) {//升级超级vip不可选择
                    showPopupMenu(lyOoutmoneybizhongtop);
                } else {
                    preenter.getSuperColorEggData();
                }
                break;
            case R.id.tv_buycommit:

                if (EmptyUtils.isNotEmpty(eggGoodsBeans) && eggGoodsBeans.size() > 0) {
                    UserWalletData.DataBean.SysMsgBean sysMsgBean = new UserWalletData.DataBean.SysMsgBean();
                    sysMsgBean.setTitle(getString(R.string.common_warmprompt));
                    sysMsgBean.setContent(getString(R.string.common_warmprompt_isbuy));
                    DialogUtils.showInfor1(this, sysMsgBean, (pwd) -> {
                        DialogUtils.enterPayPsw(this,  (pwd1) -> {
                            preenter.getEggAddData(String.valueOf(eggGoodsBeans.get(index).getId()), pwd1);
                        });
//                        DialogUtils.enterUserinfor(this, getString(R.string.paypasw_hint_inputoldpaypsw1), InputType.TYPE_NUMBER_VARIATION_PASSWORD, (pwd1) -> {
//                            preenter.getEggAddData(String.valueOf(eggGoodsBeans.get(index).getId()), pwd1);
//                        });

                    });

                } else {
                    showToast(getString(R.string.common_loaderrritry));
                    preenter.getSuperColorEggData();
                }


                break;

        }
    }

    public void showPopupMenu(View view) {
        //参数View 是设置当前菜单显示的相对于View组件位置，具体位置系统会处理
        PopupMenu popupMenu = new PopupMenu(this, view);
        //加载menu布局
        Menu menu = popupMenu.getMenu();
        for (SuperColorEggData.DataBean.EggGoodsBean eggGoodsBean : eggGoodsBeans) {
            menu.add(eggGoodsBean.getName());
        }
        popupMenu.getMenuInflater().inflate(R.menu.menu_popmenu, menu);


        //设置menu中的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                for (int i = 0; i < eggGoodsBeans.size(); i++) {
                    if (item.getTitle().toString().equalsIgnoreCase(eggGoodsBeans.get(i).getName())) {
                        if (index == i) {
//                            showToast(getString(R.string.crowdfund_hint_warn3));
                            break;
                        }
                        index = i;
                        initView();
                        break;
                    }
                }
                return true;
            }
        });
        popupMenu.show();
    }


}
